package com.company.factory;

import java.io.FileInputStream;
import java.io.IOException;

import java.util.Properties;

public class MyFactory {

    private static MyFactory myFactory = null;

    Properties properties = new Properties();

    private MyFactory() {
        try {
            //输入文件
            properties.load(new FileInputStream("cfg.cfg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static public class MyFactoryLoad {

        static public MyFactory getInstance() {
            if(myFactory==null) {
                myFactory = new MyFactory();
            }
            return myFactory;
        }

    }

    static public MyFactory getInstance() {
        return MyFactoryLoad.getInstance();
//        if (myFactory == null) {
//            synchronized (MyFactory.class){
//                if (myFactory == null) {
//                    myFactory = new MyFactory();
//                }
//            }
//        }
//        return myFactory;
    }

    public Object getObject(String objectName) {
        String property = properties.getProperty(objectName);
        Object o = null;
        try {
            o = Class.forName(property).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
            return null;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return null;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return o;
    }


}
